package com.guogee.manager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bson.types.ObjectId;

import com.guogee.bean.Community;
import com.guogee.dao.ADao;
import com.guogee.dao.CommunityDAO;
import com.guogee.db.DBHelper.CommunityCollection;

public class CommunityManager {
	private static CommunityManager mInstance;
	
	private ADao<Community> dao;
	public static CommunityManager getInstance(){
		if(mInstance == null){
			synchronized (CommunityManager.class) {
				mInstance = new CommunityManager();
			}
		}
		return mInstance;
	}
	
	public CommunityManager() {
		// TODO Auto-generated constructor stub
		dao = new CommunityDAO();
	}
	public void saveCommunity(List<Community> communityList){
		for(Community c:communityList){
			if(checkCommunityExists(c.getId())){
				Map<String,Object> con = new HashMap<String,Object>();
				con.put(CommunityCollection.UUID, new ObjectId(c.getUuid()));
				
				Map<String,Object> value = new HashMap<String,Object>();
				value.put(CommunityCollection.NAME, c.getName());
				value.put(CommunityCollection.PROVINCE, c.getProvince());
				value.put(CommunityCollection.COUNTRY, c.getCountry());
				value.put(CommunityCollection.CITY, c.getCity());
				value.put(CommunityCollection.REGION, c.getRegion());
				dao.updateItemByFeiled(con, value);
				
			}else{
				dao.insertItem(c);
			}
			
		}
	}
	
	/**
	 * 根据小区id检查小区是否已存在
	 * @param id
	 * @return
	 */
	public boolean checkCommunityExists(String uuid){
		Map<String,Object> con = new HashMap<String,Object>();
		con.put(CommunityCollection.UUID, uuid);
		Community obj = dao.queryOne(con);
		if(obj == null){
			return false;
		}
		return true;
	}
}
